﻿/***************************************************************************************************
 * Filename:            Cryptography.cs
 * Module:              加密字符串
 * Copyright:           2008 穗联软件 版权所有
 * Author:              吴潮槟
 * Created Date:        2008-09-18
 * Last Modified Data:  
 * Description:         
***************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.Diagnostics;

namespace GCELibrary.Current
{
    /// <summary>
    /// 一个加密类
    /// </summary>
    /// <remarks>
    /// 封装常用的加密算法
    /// </remarks>
    public class Cryptography
    {
 
        private const string _sKey = "SLqJzGEh6hESZD";
 

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>加密后并经base64编码的字符串</returns>
        /// <remarks>静态方法，采用默认ascii编码</remarks>
        public static string Encrypt(string strValue, string strKey)
        {
            return Encrypt3DES(strValue, strKey + _sKey);
        }

        /// <summary>
        /// 3des加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>加密后并经base64编码的字符串</returns>
        /// <remarks>静态方法，采用默认ascii编码</remarks>
        protected static string Encrypt3DES(string strValue, string strKey)
        {
            return Encrypt3DES(strValue, strKey, new ASCIIEncoding());
        }

        /// <summary>
        /// 3des加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>加密后并经base64编码的字符串</returns>
        /// <remarks>重载，指定编码方式</remarks>
        protected static string Encrypt3DES(string strValue, string strKey, Encoding encoding)
        {

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

            DES.Key = hashMD5.ComputeHash(encoding.GetBytes(strKey));
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = encoding.GetBytes(strValue);
            string result = Convert.ToBase64String(DESEncrypt.TransformFinalBlock
                (Buffer, 0, Buffer.Length));
            return System.Web.HttpUtility.UrlEncode(result);
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="a_strString">要解密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>解密后的字符串</returns>
        /// <exception cref="">密钥错误</exception>
        /// <remarks>静态方法，采用默认ascii编码</remarks>
        public static string Decrypt(string strValue, string strKey)
        {
            return Decrypt3DES(strValue, strKey + _sKey);
        }

        /// <summary>
        /// 3des解密字符串
        /// </summary>
        /// <param name="a_strString">要解密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>解密后的字符串</returns>
        /// <exception cref="">密钥错误</exception>
        /// <remarks>静态方法，采用默认ascii编码</remarks>
        protected static string Decrypt3DES(string strValue, string strKey)
        {

            return Decrypt3DES(strValue, strKey, new ASCIIEncoding());
        }//end method

        /// <summary>
        /// 3des解密字符串
        /// </summary>
        /// <param name="a_strString">要解密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>解密后的字符串</returns>
        /// <exception cref="">密钥错误</exception>
        /// <remarks>静态方法，指定编码方式</remarks>
        protected static string Decrypt3DES(string strValue, string strKey, Encoding encoding)
        {
            TripleDESCryptoServiceProvider DES = new
                TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();

            DES.Key = hashMD5.ComputeHash(encoding.GetBytes(strKey));
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESDecrypt = DES.CreateDecryptor();

            string result = "";
            try
            {
                byte[] Buffer = Convert.FromBase64String(strValue);
                result = encoding.GetString(DESDecrypt.TransformFinalBlock
                    (Buffer, 0, Buffer.Length));
            }
            catch (Exception e)
            {
#if DEBUG
                Console.WriteLine("错误：{0}", e);
#endif//DEBUG
                throw (new Exception("编码错误", e));
            }

            return System.Web.HttpUtility.UrlDecode(result);
        }//end method

        #region EncodeMD5
        /// <summary>
        /// MD5加密字符串
        /// </summary>
        /// <param name="aPwd">要加密的密码</param>
        /// <returns>加密后的密码字符串</returns>
        public static string EncodeMD5(string passWord)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(passWord, "MD5");
            //return System.Security.Cryptography.MD5.Create(passWord).ToString();
        }
        #endregion

        #region EncodeSHA1
        /// <summary>
        /// SHA1 加密字符串
        /// </summary>
        /// <param name="aPwd">要加密的密码</param>
        /// <returns>返回SHA1加密后的字符串</returns>
        public static string EncodeSHA1(string passWord)
        {
            //return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(aPwd, "SHA1");
            return System.Security.Cryptography.SHA1.Create(passWord).ToString();
        }
        #endregion
    }
}
